# from rest_framework.generics import ListAPIView,CreateAPIView
from rest_framework.generics import ListCreateAPIView
from users.models import User
from meiduo_admin.serializers.userserializer import UserSerializer
from rest_framework.permissions import IsAdminUser
from meiduo_admin.utils import LargeResultsSetPagination


# class UserView (ListAPIView,CreateAPIView):
class UserView (ListCreateAPIView):
    # 序列化器
    serializer_class = UserSerializer
    # queryset =User.objects.all()
    # 分页器
    pagination_class = LargeResultsSetPagination
    # 权限
    permission_classes = [IsAdminUser]

    # # 重写父类中判断是否传分页参数方法,
    #     def paginate_queryset(self, queryset):
    #         return queryset
    #
    # 重写父类方法
    def get_queryset(self):
        if self.request.query_params.get ("keyword") == "":
            return User.objects.all ()
        else:
            return User.objects.filter (username__contain=self.request.query_params.get ("keyword"))
